package gsw.tool.ui.shader;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ComposeShader;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.RadialGradient;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import gsw.tool.ui.R;

/**
 * 渐变效果
 */
public class MyGradientView extends View {
    private Paint mPaint;
    private Bitmap mBitMap = null;

    private int mWidth;
    private int mHeight;
    private int[] mColors = {Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW};

    public MyGradientView(Context context) {
        super(context);
        mBitMap = ((BitmapDrawable) getResources().getDrawable(R.drawable.shader_girl)).getBitmap();
        mPaint = new Paint();
        mWidth = mBitMap.getWidth();
        mHeight = mBitMap.getHeight();
    }

    public MyGradientView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mBitMap = ((BitmapDrawable) getResources().getDrawable(R.drawable.shader_girl)).getBitmap();
        mPaint = new Paint();
        mWidth = mBitMap.getWidth();
        mHeight = mBitMap.getHeight();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(Color.WHITE);

        onDrawBitmapShader(canvas);
        onDrawRadialGradient(canvas);
//        onDrawComposeShader(canvas);
    }


    /**
     * 使用BitmapShader(位图渲染器)来画
     */
    private void onDrawBitmapShader(Canvas canvas) {
        /**
         * 位图渲染器
         *
         * @param bitmap 需要渲染的位图
         * @param tileX X方向的渲染规则，包含以下3种
         *    TileMode.CLAMP 拉伸最后一个像素去铺满剩下的地方
         *    TileMode.MIRROR 通过镜像翻转铺满剩下的地方。
         *    TileMode.REPEAT 重复图片平铺整个画面（电脑设置壁纸）
         * @param tileY Y方向的渲染规则，同上
         */
        BitmapShader bitMapShader = new BitmapShader(mBitMap, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
        mPaint.setShader(bitMapShader);
        mPaint.setAntiAlias(true);
        //设置像素矩阵，来调整大小，为了解决宽高不一致的问题。
        float scale = Math.max(mWidth, mHeight) / Math.min(mWidth, mHeight);

        Matrix matrix = new Matrix();
        matrix.setScale(scale, scale);
        bitMapShader.setLocalMatrix(matrix);

        //画圆
        //canvas.drawCircle(mHeight / 2, mHeight / 2, mHeight / 2, mPaint);
        //画椭圆
        //canvas.drawOval(new RectF(0, 0, mWidth, mHeight), mPaint);
        //画矩形
        canvas.drawRect(new Rect(0, 0, 1000, 1600), mPaint);
    }

    /**
     * 使用RadialGradient(环形渲染器)绘制
     */
    private void onDrawRadialGradient(Canvas canvas) {
        int[] colors = new int[]{0x22ffffff, 0xffffffff, 0x22ffffff};
        /**
         * 渐变渲染器
         *
         * @param centerX  中心点的X坐标
         * @param centerY  中心点的X坐标
         * @param radius   园的半径
         * @param colors   渐变的颜色数组
         * @param stops    如果为null，则颜色均匀分布；否则，使用colors中的颜色
         * @param tileMode 渲染模式
         */
        RadialGradient radial = new RadialGradient(0, 0, 10, colors, null, Shader.TileMode.REPEAT);
    }

    /**
     * 用ComposeShader(组合渲染器)即可实现心形图渐变效果
     */
    private void onDrawComposeShader(Canvas canvas) {

        //创建BitmapShader，用以绘制心
        Bitmap mBitmap = ((BitmapDrawable) getResources().getDrawable(R.drawable.shader_heart)).getBitmap();
        BitmapShader bitmapShader = new BitmapShader(mBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
        //创建LinearGradient，用以产生从左上角到右下角的颜色渐变效果
        LinearGradient linearGradient = new LinearGradient(0, 0, mWidth, mHeight, Color.GREEN, Color.BLUE, Shader.TileMode.CLAMP);
        /**
         * 组合渲染器
         *
         * @param shaderA  目标渲染器
         * @param shaderB  源渲染器
         * @param mode     组合模式。PorterDuff.Mode.MULTIPLY代表颜色混合
         */
        ComposeShader composeShader = new ComposeShader(bitmapShader, linearGradient, PorterDuff.Mode.MULTIPLY);
        //当有3个以上的渲染器时，先对2个进组合，在对组合后的渲染器和第三个进行组合
        //ComposeShader composeShader2 = new ComposeShader(composeShader, linearGradient, PorterDuff.Mode.MULTIPLY);
        //将组合的composeShader作为画笔paint绘图所使用的shader
        mPaint.setShader(composeShader);

        //用composeShader绘制矩形区域
        canvas.drawRect(0, 0, mBitmap.getWidth(), mBitmap.getHeight(), mPaint);
    }
}
